package com.cpx.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Auther: PanBin
 * @Date: 2018-11-16 14:25
 * @Description: 定制的FreeMarkerVies
 * @Version: 1.0
 */
public class MyFreeMarkerView extends FreeMarkerView {

    private static Logger logger = LogManager.getLogger(MyFreeMarkerView.class);

    private static final String CONTEXT_PATH = "base";

    @Override
    protected void exposeHelpers(Map<String, Object> model,HttpServletRequest request) throws Exception {
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String path = request.getContextPath();
        //解决zuul网关的path路径侵入问题
        String zuulPath = request.getHeader("x-forwarded-prefix");
        if (zuulPath == null) zuulPath = "";
        String basePath = scheme + "://" + serverName + ":" + port + zuulPath + path;
        model.put(CONTEXT_PATH, basePath);
        logger.info("===========" + basePath);
        super.exposeHelpers(model, request);
    }
}
